package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.system.dao.UserUnderDao;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.model.UserUnder;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 下属管理 Service类
 * @author y
 */
@Service
public class UserUnderService extends BaseService<UserUnder> {
	@Resource
	private UserUnderDao userUnderDao;
	@Resource
	private SysUserService sysUserService;

	/**
	 * 添加用户上级信息
	 * @param userId        用户id
	 * @param superiorIds   上级id
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addUserSuperior(String userId , String[] superiorIds){
        this.userUnderDao.delByUnderUserId(userId);
        SysUser sysUser = sysUserService.getOneById(userId);
	    if(null != superiorIds){
			for (String superiorId : superiorIds) {
				if(userId.equals(superiorId)){
					continue;
				}
				UserUnder userUnder = new UserUnder();
				userUnder.setId(UUIDUtils.getUUIDFor32());
				userUnder.setUnderUserId(userId);
				userUnder.setUnderUserName(sysUser.getFullName());
				userUnder.setUserId(superiorId);
				this.userUnderDao.add(userUnder);
			}
		}
	}

	/**
	 * 添加用户下级信息
	 * @param userId         用户id
	 * @param underIds       下级id
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addUserUnder(String userId , String[] underIds){
	    if(null == underIds || underIds.length == 0){
	        return;
        }
	    Map<String,Object> map = new HashMap<>();
		map.put("userId",userId);
		List<String> userUnderIdList =  queryUnderByUserId(map).stream().map((userUnder)->userUnder.getUnderUserId()).collect(Collectors.toList());
        for (String underId : underIds) {
            //判断是否将自己添加为下级
            if(userId.equals(underId)){
                continue;
            }
            if(userUnderIdList.contains(underId)){
            	continue;
			}
            SysUser sysUser = sysUserService.getOneById(underId);
            UserUnder userUnder = new UserUnder();
            userUnder.setUnderUserId(underId);
            userUnder.setUnderUserName(sysUser.getFullName());
            userUnder.setId(UUIDUtils.getUUIDFor32());
            userUnder.setUserId(userId);
            this.userUnderDao.add(userUnder);
        }
	}

	/**
	 * 根据用户id 删除当前用户关联信息
	 * @param userId 用户id
	 */
	@Transactional(rollbackFor = Exception.class)
	public void delByUpOrDown(String userId){
		this.userUnderDao.delByUpOrDown(userId);
	}

	/**
	 * 根据用户id 获取下属
	 * @param queryFilter 参数
	 * @return list
	 */
	public List<UserUnder> queryUnderByUserId(QueryFilter queryFilter){
		return this.userUnderDao.queryUnderByUserId(queryFilter);
	}
	/**
	 * 根据用户id 获取下属
	 * @param map 参数
	 * @return list
	 */
	public List<UserUnder> queryUnderByUserId(Map map){
		return this.userUnderDao.queryUnderByUserId(map);
	}
	/**
	 * 根据用户id 获取上级
	 * @param userId 用户id
	 * @return list
	 */
	public List<UserUnder> querySuperiorByUserId(String userId){
		return this.userUnderDao.querySuperiorByUserId(userId);
	}

	/**
	 * 获取我的下属用户ID集合。
	 * @param userId
	 * @return
	 */
	public Set<String> getMyUnderUserId(String userId){
		Set<String> list=new HashSet<String>();
		List<UserUnder> listUser= userUnderDao.getMyUnderUser(userId);
		for(UserUnder user:listUser){
			list.add(user.getUnderUserId());
		}
		return list;
	}
}
